package com.example.diary

import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_edit.*
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.activity_main.*
import java.text.SimpleDateFormat
import java.util.*


class EditActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit)
        val dbHelper =MyDatabaseHelper(this,"diary.db",1)
        val db=dbHelper.writableDatabase
        btn_save.setOnClickListener {
            Toast.makeText(this, "123！", Toast.LENGTH_SHORT).show()
            val t = edit_title.text.toString()
            val c = edit_content.text.toString()
            val sdf=SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val time=sdf.format(Date())

            val values1 = ContentValues().apply {
                // 开始组装第一条数据
                put("user", "a")
                put("title",t )
                put("content",c)
                put("time",time )
            }

            db.insert("DiaryData", null, values1) // 插入第一条数据
            Toast.makeText(this, "日记添加成功！", Toast.LENGTH_SHORT).show()

            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
        btn_quit.setOnClickListener {
            Toast.makeText(this, "退出到主页面", Toast.LENGTH_SHORT).show()
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
    }
}
